﻿using ModuleBase.googoltech.GTS;
using ModuleBase;
using Newtonsoft.Json;
using Prism.Ioc;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Windows.Media.Media3D;

namespace InitAxis
{
    public class InitAxis : ModuleBase.ModuleBase
    {
        private IAxisCardService _axisCardService;
        private IContainerProvider _containerProvider;
        bool outputs = false;
        bool isExecuted = false;
        private int _parameter1 = 8;
        public int Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }
        private string _parameter2 = "Top_A8";
        public string Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        private string _parameter3 = "Top_A8";
        public string Parameter3
        {
            get => _parameter3;
            set
            {
                if (_parameter3 != value)
                {
                    _parameter3 = value;
                    OnPropertyChanged(nameof(Parameter3));
                }
            }
        }

        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "轴数", Parameter1 },
                { "轴卡参数名", Parameter2 },
                { "轴卡配置名", Parameter3 },
            };
        }

        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "轴数", new List<object> {  4,8 } },
        };

        public InitAxis(IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
            Name = "固高轴卡初始化";

            DisplayNameToPropertyNameMap = new Dictionary<string, string>
            {
                { "轴数", nameof(Parameter1) },
                { "轴卡参数名", nameof(Parameter2) },
                { "轴卡配置名", nameof(Parameter3) },
            };
        }

        public override bool Execute()
        {
            Status = ModuleStatus.Executing;

            if (isExecuted)
            {
                outputs = true;
                return outputs;
            }


            var containerExtension = _containerProvider.Resolve<IContainerExtension>();
            if (!_containerProvider.IsRegistered<IAxisCardService>(Parameter3))
            {
                containerExtension.RegisterSingleton<IAxisCardService, AxisCardService>(Parameter3);
            }

            _axisCardService = _containerProvider.Resolve<IAxisCardService>(Parameter3);

            bool gtsCardInitResult = _axisCardService.Init(Parameter1, Parameter2, Parameter3);
            if (!gtsCardInitResult)
            {
                outputs = false;
                Status = ModuleStatus.Failed;
            }
            else
            {
                outputs = true;
                isExecuted = true;
            }

            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {
            outputs = false;
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {
            isExecuted = false;
        }

        public override string Description => "固高轴卡初始化模块：用于初始化指定数量的固高轴卡，完成轴卡参数和配置注册。";
    }
}